/**
 * Created by siguangyi on 17/4/19.
 */
var time = 120;
var timeTotal = 0;
var hasGetReport=false;
var timer=null;
var canvas = document.getElementById("waitCanvas");
var context = canvas.getContext('2d');

function timedCount(){
    $("#reportDetail").css("display","none");
    $("#reportFailed").css("display","none");

    if(timer)
        return;

    FlushWaitTime();
    timer = setInterval(function(){
        timeTotal++;
        if( time-- <= 0||hasGetReport){
            CloseTimer();

            if(!hasGetReport)
            {
                QueryFailed();
            }
        }
        else
        {
            FlushWaitTime();
        }

        if(timeTotal > 30)
        {
            if(time%5 == 0)
            {
                QueryReport();
            }
        }

    },1000);
}

function QueryReport() {
    var url="/gaigo-open-shre/api/task/openQueryReport.htm";
    var listener=dealQuery;

    var params={};
    params.taskId=taskId;
    params.phone=phone;
    params.sysParam = mSysParam;
    params.userInfor = mUserInfor;

    Request("POST",JSON.stringify(params),url,listener);
}

function CloseTimer() {
    if(timer)
    {
        clearInterval(timer);
        timer = null;
    }
}

function dealQuery(resultObject){
    if(resultObject.code==200){
        var reportId = resultObject.data;
        if(typeof(reportId) != "undefined")
        {
            hasGetReport=true;
            CloseTimer();
            FlushWaitTime();

            $("#reportDetail").css("display","inline-block");
            $("#reportFailed").css("display","none");

            $("#reportDetail").click(function(){
            	GetReportDetail(reportId);
            	});
        }

    }
}

function QueryFailed() {
    $("#reportDetail").css("display","none");
    $("#reportFailed").css("display","inline-block");

    $("#reportFailed").click(function(){
        time = 60;
        timedCount();
    });
}

function GetReportDetail(reportId)
{
	var param = {};
	param.reportId  = reportId;
	param.sysParam = mSysParam;
	param.taskId   = taskId;

    var url="/gaigo-open-shre/api/task/openGetReportDetail.htm";
    var listener=getReportDetail;

    Request("POST",JSON.stringify(param),url,listener);
}

function getReportDetail(result)
{
	if(result.code == "200")
	{
		var URL = result.data;
		
		window.location.replace(URL);		
	}
}

function FlushWaitTime() {
    context.clearRect(10, 10, 180, 180);

    context.lineWidth=1;
    context.strokeStyle="#024c6e";

    context.beginPath();
    context.arc(105,105,100,0,2*Math.PI);
    context.stroke();

    context.font = "bold 58pt Arial";  // 字体大小，样式
    context.fillStyle = "#333";  // 颜色
    context.textAlign = 'center';  // 位置
    if(hasGetReport)
    {
        context.fillText(timeTotal, 90, 110);

        context.font = "bold 20pt Arial";  // 字体大小，样式
        context.fillText("秒", 145, 110);

    }
    else {
        context.fillText(time, 104, 110);
    }

    context.font = "bold 12pt Arial";  // 字体大小，样式
    context.fillStyle = "#dd3a00";  // 颜色

    if(hasGetReport)
    {
        context.fillText("报告已生成", 110, 170);
    }
    else {
        context.fillText("报告生成中...", 108, 170);
    }

}